package pkg.customer;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.hibernate.Transaction;

import retail.bo.CustomerBo;

public class ForgotPasswordAction extends Action {
	private CustomerBo customerBo;
	
	public CustomerBo getCustomerBo() {
		return customerBo;
	}

	public void setCustomerBo(CustomerBo customerBo) {
		this.customerBo = customerBo;
	}
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		DynaActionForm myform = (DynaActionForm) form;
		
		String name=(String)myform.get("user_name");
     	String id=(String)myform.get("user_id");	                                                         
     	//use spring method to count number of users with inputted username "name
     	System.out.println(name);
     	if(customerBo.countUser(name,id)==1)
     	{
     		if(id.startsWith("R")){
     			session.setAttribute("username",name);
     			session.setAttribute("userid", id);
     			return mapping.findForward("Rep");
     		}
     		if(id.startsWith("C")){
     			session.setAttribute("username", name);
     			session.setAttribute("userid", id);
     	     	String SecurityQuestion=customerBo.getSecurityQuestion(id);
     	     	session.setAttribute("SQ",SecurityQuestion);
     			return mapping.findForward("Customer");
     		}
     		else{
     			session.setAttribute("errMsg", "Enter proper user ID");
     			return mapping.findForward("Error");
     		}
     	}
     	else{
     	    session.setAttribute("errMsg","Username "+name+", UserID "+id+" not proper: " + customerBo.countUser(name,id));
     		return mapping.findForward("Error");
     	}
	}     
}
//		<!--    <c:choose>-->
//		<!---->
//		<!--        <c:when test="${fn:startsWith(param.uid,'R0')}">-->
//		<!--            <sql:query dataSource="jdbc/myoracle" var="rep_user">select count(*) as cnt from repdetails where user_name='${param.uname}'</sql:query>-->
//		<!--            <c:if test="${rep_user.rows[0].cnt>0}">-->
//		<!--                <% session.setAttribute( "uname", (String) request.getParameter( "uname")); %>-->
//		                    redirect to the rep forgot password page
//		<!--                    <c:redirect url="ForgotPassword_Rep.jsp"/>-->
//		<!--            </c:if>-->
//		<!--            <c:if test="${rep_user.rows[0].cnt==0}">-->
//		<!--            	<div align="center">-->
//		<!--                	<p>please enter the correct username</p>-->
//		<!--                </div>-->
//		<!--            </c:if>-->
//		<!--        </c:when>-->
//		<!---->
//		<!---->
//		<!--        <c:when test="${(fn:startsWith(param.uid,'C0'))}">-->
//		<!--            <sql:query dataSource="jdbc/myoracle" var="rep_user">select count(*) as cnt from users where user_name='${param.uname}'</sql:query>-->
//		<!--	            <c:if test="${rep_user.rows[0].cnt>0}">-->
//		<!--	                <% session.setAttribute( "uname", (String) request.getParameter( "uname")); %>-->
//			                redirect to the rep forgot password page
//		<!--	                <c:redirect url="ForgotPassword_Customer.jsp"/>-->
//		<!--	            </c:if>-->
//		<!--            <c:if test="${rep_user.rows[0].cnt==0}">-->
//		<!--            	<div align="center">-->
//		<!--               		<p>please enter the correct username</p>-->
//		<!--               	</div>-->
//		<!--            </c:if>-->
//		<!--        </c:when>-->
//		<!---->
//		<!--		<c:when test="${(not (fn:startsWith(param.uid,'C') and fn:startsWith(param.uid,'R'))) and (not empty param.uid)}">-->
//		<!--		<div align="center">-->
//		<!--			<p> enter proper user id </p>-->
//		<!--		</div>-->
//		<!--		</c:when>-->
//		<!--		<c:when test="${((empty param.uname) or (empty param.uid))}">-->
//		<!--		<div align="center">-->
//		<!--			<p> please enter both username and user id </p>-->
//		<!--		</div>-->
//		<!--		</c:when>-->
//		<!--        <c:otherwise>-->
//		<!--        </c:otherwise>-->
//		<!--    </c:choose>-->